package cn.scw.project.facility.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.facility.domain.FacPrintType;
import cn.scw.project.facility.domain.netework.bean.FacPrintTypeAddReq;
import cn.scw.project.facility.mapper.FacPrintTypeMapper;
import cn.scw.project.facility.service.IFacPrintTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 打印类型Service业务层处理
 *
 * @author api
 * @date 2020-06-02
 */
@Service
public class FacPrintTypeServiceImpl implements IFacPrintTypeService {
    @Autowired
    private FacPrintTypeMapper facPrintTypeMapper;

    /**
     * 查询打印类型
     *
     * @param id 打印类型ID
     * @return 打印类型
     */
    @Override
    public FacPrintType selectFacPrintTypeById(Long id) {
        return facPrintTypeMapper.selectFacPrintTypeById(id);
    }

    /**
     * 查询打印类型列表
     *
     * @return 打印类型
     */
    @Override
    public List<FacPrintType> selectFacPrintTypeList( ) {
        return facPrintTypeMapper.selectFacPrintTypeList();
    }

    /**
     * 新增打印类型
     *
     * @param req 打印类型
     * @return 结果
     */
    @Override
    public int insertFacPrintType(FacPrintTypeAddReq req) {
        if (StringUtils.isBlank(req.getCode()))
            throw new CustomException("请输入code");
        if (this.existsByCode(req.getCode()))
            throw new CustomException("code已存在");
        if (StringUtils.isAnyBlank(req.getType(), req.getSize()))
            throw new CustomException("请输入类型和大小");
        FacPrintType facPrintType = new FacPrintType();
        ToolUtils.copyPropertiesIgnoreNull(req, facPrintType);
        return facPrintTypeMapper.insertFacPrintType(facPrintType);
    }

    /**
     * 修改打印类型
     *
     * @param req 打印类型
     * @return 结果
     */
    @Override
    public int updateFacPrintType(FacPrintTypeAddReq req) {
        FacPrintType facPrintType=this.selectFacPrintTypeById(req.getId());
        if (facPrintType==null)
            throw new CustomException("类型不存在");
        if (StringUtils.isNotBlank(req.getCode())&&!req.getCode().equals(facPrintType.getCode())){
            if (this.existsByCode(req.getCode()))
                throw new CustomException("code已存在");
        }
        ToolUtils.copyPropertiesIgnoreNull(req,facPrintType);
        if (StringUtils.isAnyBlank(facPrintType.getType(), facPrintType.getSize()))
            throw new CustomException("请输入类型和大小");
        return facPrintTypeMapper.updateFacPrintType(facPrintType);
    }

    /**
     * 批量删除打印类型
     *
     * @param ids 需要删除的打印类型ID
     * @return 结果
     */
    @Override
    public int deleteFacPrintTypeByIds(Long[] ids) {
        return facPrintTypeMapper.deleteFacPrintTypeByIds(ids);
    }

    /**
     * 删除打印类型信息
     *
     * @param id 打印类型ID
     * @return 结果
     */
    @Override
    public int deleteFacPrintTypeById(Long id) {
        return facPrintTypeMapper.deleteFacPrintTypeById(id);
    }

    @Override
    public boolean existsByCode(String code) {
        return facPrintTypeMapper.existsByCode(code);
    }
}
